/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.catosist.run.dataaccess.domain.hibernate;

import com.catosist.run.dataaccess.base.hibernate.HibernateGenericDAO;
import com.catosist.run.dataaccess.domain.ComprobanteDao;
import com.catosist.run.dataaccess.model.ComprobanteVO;
import com.catosist.run.dataaccess.model.ResultadoVentaVO;
import java.io.Serializable;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
/**
 *
 * @author igorov
 */
public class HibernateComprobanteDao extends HibernateGenericDAO<ComprobanteVO, Integer> implements ComprobanteDao{
    public HibernateComprobanteDao() {
        super(ComprobanteVO.class);
    }

    public List<ComprobanteVO> getByViajeId(int viajeId) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ComprobanteVO.class);
        detachedCriteria.add(Restrictions.eq("viaje.id", viajeId));
//        detachedCriteria.addOrder(Order.desc("fingreso"));
        List<ComprobanteVO> comprobanteVOs = listByCriteria(detachedCriteria);
        System.out.println("Cantidad dao: "+comprobanteVOs.size()+viajeId);
        return comprobanteVOs;
    }

    public List<ComprobanteVO> getByNumero(int numero) {
        return this.getSession().
                createSQLQuery("SELECT * FROM comprobante where numero = " + numero +";").
                addEntity(ComprobanteVO.class).list();
    }

    public List<ComprobanteVO> getAllFacturas() {
        return this.getSession().
                createSQLQuery("select * from comprobante where tipo_comprobante = 'f';").
                addEntity(ComprobanteVO.class).list();
    }

    public List<ComprobanteVO> getAllBoletas() {
        return this.getSession().
                createSQLQuery("select * from comprobante where tipo_comprobante = 'b';").
                addEntity(ComprobanteVO.class).list();
    }

    public List<ComprobanteVO> getAllContratos() {
        return this.getSession().
                createSQLQuery("select * from comprobante where tipo_comprobante = 'c';").
                addEntity(ComprobanteVO.class).list();
    }



    public List<ComprobanteVO> getAllGroupBySerie() {
        return this.getSession().
                createSQLQuery("SELECT * FROM comprobante group by numeroserie;").
                addEntity(ComprobanteVO.class).list();
    }


    public List<Object> reporteVentas() {
        return this.getSession().
                createSQLQuery("SELECT Year(comprobante.fingreso) as `anho`," +
                "Month(comprobante.fingreso) as `mes`," +
                "SUM(comprobante.monto) as `montos`," +
                "SUM(comprobante.igv) as `igvs`," +
                "SUM(comprobante.totalsindetraccion) as `subtotales`," +
                "SUM(comprobante.detraccion) as `detracciones`," +
                "SUM(comprobante.total) as `totales`" +
                "FROM comprobante group by Year(comprobante.fingreso)," +
                "Month(comprobante.fingreso);").list();
    }

    public List<Integer> getAllGroupByAnho() {
        return this.getSession().
                createSQLQuery("SELECT Year(comprobante.fingreso) as `anho`" +
                "FROM comprobante group by Year(comprobante.fingreso);").list();
    }
}
